﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PB.Airsoft.Business.User;
using PB.Airsoft.Business.User.Interfaces;
using PB.Airsoft.NHibernateWrapper;
using System.Configuration;
using PB.Airsoft.Core.Unity;
using NHibernate.Criterion;

namespace PB.Airsoft.Provider.NHibernate.User
{
    public class NHibernateUserProvider : UserProvider
    {
        private ISessionManager CreateSessionManager(bool readOnly)
        {
            return new SessionManager(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString, readOnly);
        }

        public override IUser CreateUser(string name)
        {
            var user = IoC.GetInstance<IUser>();
            user.Id = Guid.NewGuid();
            user.Name = name;

            using (var sm = CreateSessionManager(false))
            {
                sm.Session.Save(user);
                sm.Commit();
            }

            return user;
        }

        public override IUser GetUser(Guid id)
        {
            using (var sm = CreateSessionManager(true))
            {
                return (IUser)sm.Session.Get(IoC.GetRegisteredType<IUser>(), id);
            }
        }

        public override void IncrementMoney(Guid userId, decimal money)
        {
            using (var sm = CreateSessionManager(false))
            {
                var user = (IUser)sm.Session.Get(IoC.GetRegisteredType<IUser>(), userId);
                user.Money += money;

                sm.Commit();
            }
        }

        public override void AddItem(Guid userId, Guid itemId)
        {
            using (var sm = CreateSessionManager(false))
            {
                var userInventoryItem = IoC.GetInstance<IUserInventoryItem>();
                userInventoryItem.Id = Guid.NewGuid();
                userInventoryItem.UserId = userId;
                userInventoryItem.ItemId = itemId;

                sm.Session.Save(userInventoryItem);

                sm.Commit();
            }
        }

        public override void RemoveItem(Guid userId, Guid itemId)
        {
            using (var sm = CreateSessionManager(false))
            {
                var criteria = sm.Session.CreateCriteria(IoC.GetRegisteredType<IUserInventoryItem>());
                criteria.Add(Expression.Eq("UserId", userId));
                criteria.Add(Expression.Eq("ItemId", itemId));
                
                sm.Session.Delete(criteria.List<IUserInventoryItem>()[0]);
                
                sm.Commit();
            }
        }

        public override List<IUserInventoryItem> GetUserInventoryItems(Guid userId)
        {
            using (var sm = CreateSessionManager(true))
            {
                var criteria = sm.Session.CreateCriteria(IoC.GetRegisteredType<IUserInventoryItem>());
                criteria.Add(Expression.Eq("UserId", userId));
                return criteria.List<IUserInventoryItem>().ToList();
            }
        }
    }
}
